home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / mxcode / adpcmsrc / adpcm.c < prev    next >
C/C++ Source or Header  |  1993-10-23  |  8KB  |  259 lines

  1. /***********************************************************
  2. Copyright 1992 by Stichting Mathematisch Centrum, Amsterdam, The
  3. Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /*
  26. ** Intel/DVI ADPCM coder/decoder.
  27. **
  28. ** The algorithm for this coder was taken from the IMA Compatability Project
  29. ** proceedings, Vol 2, Number 2; May 1992.
  30. **
  31. ** Version 1.0, 7-Jul-92.
  32. */
  33.  
  34. #include "adpcm.h"
  35.  
  36. #ifndef __STDC__
  37. #define signed
  38. #endif
  39.  
  40. #define NODIVMUL
  41.  
  42. /* Intel ADPCM step variation table */
  43. static long indexTable[16] = {
  44.     -1, -1, -1, -1, 2, 4, 6, 8,
  45.     -1, -1, -1, -1, 2, 4, 6, 8,
  46. };
  47.  
  48. static long stepsizeTable[89] = {
  49.     7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
  50.     19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
  51.     50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
  52.     130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
  53.     337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
  54.     876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
  55.     2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
  56.     5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
  57.     15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
  58. };
  59.  
  60. void
  61. adpcm_coder(indata, outdata, len, state)
  62.     short indata[];
  63.     char outdata[];
  64.     long len;
  65.     struct adpcm_state *state;
  66. {
  67.     short *inp;                        /* Input buffer pointer */
  68.     signed char *outp;         /* output buffer pointer */
  69.     long val;                   /* Current input sample value */
  70.     long sign;                  /* Current adpcm sign bit */
  71.     long delta;                 /* Current adpcm output value */
  72.     long step;                  /* Stepsize */
  73.     long valprev;               /* virtual previous output value */
  74.     long vpdiff;                        /* Current change to valprev */
  75.     long index;                 /* Current step change index */
  76.     long outputbuffer;          /* place to keep previous 4-bit value */
  77.     long bufferstep;            /* toggle between outputbuffer/output */
  78.  
  79.     outp = (signed char *)outdata;
  80.     inp = indata;
  81.  
  82.     valprev = state->valprev;
  83.     index = state->index;
  84.     step = stepsizeTable[index];
  85.  
  86.     bufferstep = 1;
  87.  
  88.     for ( ; len > 0 ; len-- ) {
  89.        val = *inp++;
  90.  
  91.        /* Step 1 - compute difference with previous value */
  92.        delta = val - valprev;
  93.        sign = (delta < 0) ? 8 : 0;
  94.        if ( sign ) delta = (-delta);
  95.  
  96.        /* Step 2 - Divide and clamp */
  97. #ifdef NODIVMUL
  98.         {
  99.            long tmp = 0;
  100.  
  101.            vpdiff = 0;
  102.            if ( delta > step ) {
  103.                tmp = 4;
  104.                delta -= step;
  105.                vpdiff = step;
  106.            }
  107.            step >>= 1;
  108.            if ( delta > step  ) {
  109.                tmp |= 2;
  110.                delta -= step;
  111.                vpdiff += step;
  112.            }
  113.            step >>= 1;
  114.            if ( delta > step ) {
  115.                tmp |= 1;
  116.                vpdiff += step;
  117.            }
  118.            delta = tmp;
  119.        }
  120. #else
  121.        delta = (delta<<2) / step;
  122.        if ( delta > 7 ) delta = 7;
  123.  
  124.        vpdiff = (delta*step) >> 2;
  125. #endif
  126.  
  127.        /* Step 3 - Update previous value */
  128.        if ( sign )
  129.          valprev -= vpdiff;
  130.        else
  131.          valprev += vpdiff;
  132.  
  133.        /* Step 4 - Clamp previous value to 16 bits */
  134.        if ( valprev > 32767 )
  135.          valprev = 32767;
  136.        else if ( valprev < -32768 )
  137.          valprev = -32768;
  138.  
  139.        /* Step 5 - Assemble value, update index and step values */
  140.        delta |= sign;
  141.  
  142.        index += indexTable[delta];
  143.        if ( index < 0 ) index = 0;
  144.        if ( index > 88 ) index = 88;
  145.        step = stepsizeTable[index];
  146.  
  147.        /* Step 6 - Output value */
  148.        if ( bufferstep ) {
  149.            outputbuffer = (delta << 4) & 0xf0;
  150.        } else {
  151.            *outp++ = (delta & 0x0f) | outputbuffer;
  152.        }
  153.        bufferstep = !bufferstep;
  154.     }
  155.  
  156.     /* Output last step, if needed */
  157.     if ( !bufferstep )
  158.       *outp++ = outputbuffer;
  159.  
  160.     state->valprev = valprev;
  161.     state->index = index;
  162. }
  163.  
  164. void
  165. adpcm_decoder(indata, outdata, len, state)
  166.     char indata[];
  167.     short outdata[];
  168.     int len;
  169.     struct adpcm_state *state;
  170. {
  171.     signed char *inp;          /* Input buffer pointer */
  172.     short *outp;               /* output buffer pointer */
  173.     long sign;                  /* Current adpcm sign bit */
  174.     long delta;                 /* Current adpcm output value */
  175.     long step;                  /* Stepsize */
  176.     long valprev;               /* virtual previous output value */
  177.     long vpdiff;                        /* Current change to valprev */
  178.     long index;                 /* Current step change index */
  179.     long inputbuffer;           /* place to keep next 4-bit value */
  180.     long bufferstep;            /* toggle between inputbuffer/input */
  181.  
  182.     outp = outdata;
  183.     inp = (signed char *)indata;
  184.  
  185.     valprev = state->valprev;
  186.     index = state->index;
  187.     step = stepsizeTable[index];
  188.  
  189.     bufferstep = 0;
  190.  
  191.     len *=2;
  192.  
  193.     for ( ; len > 0 ; len-- ) {
  194.  
  195.        /* Step 1 - get the delta value and compute next index */
  196.        if ( bufferstep ) {
  197.            delta = inputbuffer & 0xf;
  198.        } else {
  199.            inputbuffer = *inp++;
  200.            delta = (inputbuffer >> 4) & 0xf;
  201.        }
  202.        bufferstep = !bufferstep;
  203.  
  204.         /* Step 2 - Find new index value (for later) */
  205.         index += indexTable[delta];
  206.         if ( index < 0 )
  207.         {
  208.             index = 0;
  209.         }
  210.         else if ( index > 88 )
  211.         {
  212.             index = 88;
  213.         }
  214.  
  215.        /* Step 3 - Separate sign and magnitude */
  216.        sign = delta & 8;
  217.        delta = delta & 7;
  218.  
  219.        /* Step 4 - update output value */
  220. #ifdef NODIVMUL
  221.         vpdiff = 0;
  222.         if ( delta & 4 )
  223.         {
  224.             vpdiff  = (step << 2);
  225.         }
  226.         if ( delta & 2 )
  227.         {
  228.             vpdiff += (step << 1);
  229.         }
  230.         if ( delta & 1 )
  231.         {
  232.             vpdiff += step;
  233.         }
  234.         vpdiff >>= 2;
  235. #else
  236.         vpdiff = (delta*step) >> 2;
  237. #endif
  238.        if ( sign )
  239.          valprev -= vpdiff;
  240.        else
  241.          valprev += vpdiff;
  242.  
  243.        /* Step 5 - clamp output value */
  244.        if ( valprev > 32767 )
  245.          valprev = 32767;
  246.        else if ( valprev < -32768 )
  247.          valprev = -32768;
  248.  
  249.        /* Step 6 - Update step value */
  250.        step = stepsizeTable[index];
  251.  
  252.        /* Step 7 - Output value */
  253.        *outp++ = valprev;
  254.     }
  255.  
  256.     state->valprev = valprev;
  257.     state->index = index;
  258. }
  259.